'''
Created on Aug 5, 2009

@author: mkiyer
'''

import os
import sys
import logging

from veggie.app.chipseq import sw_root
from veggie.app.application import CommandLineApplication

class ChIPDiff(CommandLineApplication):
    '''
    Genome-wide Identification of Differential Histone Modification Sites from ChIP-Seq Data
    Xu H., Wei C.L., Lin F., Sung W.K. (2008)
    http://cmb.gis.a-star.edu.sg/ChIPSeq/paperChIPDiff.htm

    Usage:
    ChIPDiff <Library 1 Tag File Name> <Library 2 Tag File Name> \
             <Chromosome Description File Name> <Config File Name> \
             <project name>

    Example:
    ./ChIPDiff sample_L1.tag sample_L2.tag chrom_descr.txt config.txt sample
    '''
    _chipdiff_home = os.path.join(sw_root, 'ChIPDiff')
    # TODO: make genome-compatible
    _configfile = os.path.join(_chipdiff_home, "config.txt")
    _executable = os.path.join(_chipdiff_home, "ChIPDiff")

    def get_result_paths(self):
        basepath = os.path.join(os.path.abspath(self.cwd), self.exper_name)
        regionfile = basepath + '.region'        
        return {'region': basepath + '.region',
                'bin': basepath + '.bin',
                'hmm': basepath + '.hmm'}

    def run(self, t_file, c_file, chrom_sizes_file, name="CHIPDIFF", options=None, cwd=None):
        if cwd:
            self.cwd = cwd
        if options:
            self.addOptions(options)
        # append the application name to the end of the experiment name
        self.exper_name = name               
        # setup command-line parameters
        self.args = [t_file, c_file, chrom_sizes_file,
                     self._configfile, 
                     name]
        return self()

#    def run(self, exper_name, parser, treatment_file, control_file, options=None, cwd=None):
#        # append the application name to the end of the experiment name
#        # TODO: create experiment name from treatment file and control file
#        exper_name = self.__class__.__name__ + '_' + exper_name
#        self.exper_name = exper_name
#        
#        if cwd:
#            self.cwd = cwd
#        if options:
#            self.addOptions(options)
#
#        # TODO: chipdiff takes files in 'tag' format
#        # add an optional format conversion step here
#        tag_files = []
#        for file in [treatment_file, control_file]:
#            tag_file = os.path.join(self.cwd, os.path.splitext(os.path.basename(file))[0] + '.tag')
#            tag_files.append(tag_file)
#            if not os.path.exists(tag_file):
#                print file, 'converting to tag format..'
#                tag_fhd = open(tag_file, 'w')
#                convert_to_tag_format(parser, open(file, 'r'), tag_fhd)
#            else:
#                print tag_file, 'exists so skipping conversion'
#        # setup command-line parameters
#        self.args = [tag_files[0], tag_files[1],
#                     self._chromfile,
#                     self._configfile, 
#                     exper_name]
#        # run -- invokes the CommandLineApplication __call__() function
#        return self()
